home *** CD-ROM | disk | FTP | other *** search
- Path: news.bridge.net!news
- From: David Byrden <100101.2547@compuserve.com>
- Newsgroups: comp.lang.c++
- Subject: Re: How best to handle constructor failure?
- Date: 9 Jan 1996 05:42:52 GMT
- Organization: self-employed
- Message-ID: <4csv8s$9gg@news.bridge.net>
- References: <4c36gl$5e5@nnrp1.news.primenet.com> <4cimnp$hne@news1.panix.com>
- NNTP-Posting-Host: ppp-mia1-58.bridge.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
-
- Jeffrey;
-
- >> Yes, using auto pointers will avoid memory leaks from un-destructed
- >> objects. The problem, however, is how to alter the sequence of code
- >> existing OUTSIDE of the constructor, based on the success or failure
- >> of the constructor.
-
- I don't understand. You want to "bypass the code that follows the
- constructor" presumably because it will try to use the object which
- failed to construct.
-
- But surely, if a ctor fails, then it will throw an exception, and the
- exception will propagate out of it, and that will terminate the
- SURROUNDING try block, including all the dangerous code?
-
- Yes, it is possible to put try and catch blocks within the ctor, and
- thus to catch and 'swallow' an exception - but you should only swallow it
- if you can SOLVE the associated problem and succesfully construct the
- object after all.
-
- Does this answer your question?
-
-
- p.s. two-pase construction, where the ctor makes an invalid object and a
- second function must be called, is no longer a useful C++ technique; both
- for the reasons that you pointed out, AND because you cannot tell a
- template that your particular class needs a special function to be
- called.
-
-
- David
-
- || My opinions ARE those of my employer ||
-
-
-
-
-